package com.jdz.gxq.info.admin.role.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import com.jdz.gxq.info.admin.role.bo.Menu;
import com.jdz.gxq.info.admin.role.bo.Role;
import com.jdz.gxq.info.admin.role.form.RoleForm;
import com.jdz.gxq.info.admin.role.service.RoleService;
import com.jdz.gxq.info.common.action.CommonDispatchAction;
import com.jdz.gxq.info.common.bo.PageBean;

public class RoleAction extends CommonDispatchAction {

	private RoleService roleService = null;
	 
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws java.lang.Exception {
		
		RoleForm roleForm = (RoleForm) form;

		String name = roleForm.getName();

		int totalCount = roleService.getRolesCount(name);

		int currentPage = roleForm.getPageIndex();

		PageBean pageBean = new PageBean(totalCount, currentPage);

		roleForm.setPageIndex(pageBean.getCurrentPage());

		Map<String, Object> params = new HashMap<String, Object>();

		// 分页用
		params.put("startRow", pageBean.getPageStartRow() - 1);
		params.put("rowSize", pageBean.getPageSize());
		params.put("name", name);

		List<Role> roleList = roleService.getRoles(params);

		request.setAttribute("roleList", roleList);
		request.setAttribute("pageBean", pageBean);

		return mapping.findForward("success");
	}
	
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws java.lang.Exception {
		
		RoleForm roleForm = (RoleForm) form;
		
		Role role = roleForm.getRole();

		// role名重复验证(其他后台逻辑验证也类似处理)
		if (!roleService.checkRoleName(role, 1)) {
			ActionMessages messages = new ActionMessages();
			messages.add("role.projectName", new ActionMessage("error.roleNameExisted"));
			addErrors(request, messages);
			return mapping.getInputForward();
		}
		
		role.setModifiedBy(getUserId(request));
		
		roleService.insertRole(role);
		
		return mapping.findForward("success");
	}
	
	public ActionForward gotoEdit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws java.lang.Exception {
		
		RoleForm roleForm = (RoleForm) form;
		
		Role role = roleService.getRoleById(roleForm.getId());
		
		roleForm.setRole(role);

		return mapping.findForward("success");
	}
	
	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws java.lang.Exception {
		
		RoleForm roleForm = (RoleForm) form;
		
		Role role = roleForm.getRole();
		
		// role名重复验证(其他后台逻辑验证也类似处理)
		if (!roleService.checkRoleName(role, 2)) {
			ActionMessages messages = new ActionMessages();
			messages.add("role.projectName", new ActionMessage("error.roleNameExisted"));
			addErrors(request, messages);
			return mapping.getInputForward();
		}
		
		role.setModifiedBy(getUserId(request));
		
		roleService.updateRole(role);
		
		return mapping.findForward("success");
	}
	
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws java.lang.Exception {
		
		String id = request.getParameter("id");
		
		String[] ids = id.split(",");
		
		for (String item : ids) {
			roleService.deleteRole(Integer.parseInt(item));
		}

		return mapping.findForward("success");
	}
	
	public ActionForward gotoSetRole(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws java.lang.Exception {
		
		RoleForm roleForm = (RoleForm) form;

		List<Menu> mainMenuList = roleService.getRoleMenuList(roleForm.getId());
		
		request.setAttribute("mainMenuList", mainMenuList);

		return mapping.findForward("success");
	}
	
	public ActionForward setRoleMenu(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws java.lang.Exception {
		
		RoleForm roleForm = (RoleForm) form;

		roleService.setRoleMenus(roleForm.getId(), roleForm.getRoleMenuList(), getUserId(request));
		
		return mapping.findForward("success");
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}
}

